const fs = require('fs/promises');
const mime = require('mime');


function staticsController() {
    return async (ctx, next) => {
        // console.log(ctx.request.path);
        let tmpPath = ctx.request.path;
        console.log(tmpPath);
        // 判断路径中是否包含特定的字符（如static），如果是，就当静态资源处理，否则就放行继续下一个中间件
        if (tmpPath.startsWith('/statics')) {
            let fullPath=__dirname + tmpPath;
            let stat = await fs.stat(fullPath);
            console.log(stat.isFile());
            if (stat.isFile()) {
                ctx.type = mime.getType(fullPath);
                ctx.body = await fs.readFile(fullPath);
            } else {
                ctx.body.status = 404;
            }
        } else {
            await next();
        }

        // let url='./abc.html';
        // let url='./statics/imgs/imgs01.jpg';




    }
}

module.exports = staticsController